﻿using NaughtyAttributes;
using UnityEditor;
using UnityEngine;

namespace Gj
{
    public class CopyMeshBones : MonoBehaviour
    {
        public Mesh source;
        public Mesh target;
        public SkinnedMeshRenderer renderer;
        // public DefaultAsset Folder;

        [Button()]
        public void Copy()
        {
            Mesh newMesh = Instantiate(target);

// 复制 BoneWeights 和 BindPoses
            newMesh.boneWeights = source.boneWeights; // 关键：权重数据
            newMesh.bindposes = source.bindposes;     // 关键：绑定姿势

            //
            // // 生成唯一文件名
            // string meshName = "Mesh_" + System.DateTime.Now.ToString("yyyyMMdd_HHmmss");
            // string assetPath = AssetDatabase.GetAssetPath(Folder) + "/" + meshName + ".asset";
            //
            // // 保存 Mesh
            // AssetDatabase.CreateAsset(newMesh, assetPath);
            // AssetDatabase.SaveAssets();
            // AssetDatabase.Refresh();
            renderer.sharedMesh = newMesh;
        }
        
        public GameObject sourceSkinnedObject; // 带骨骼的源物体
        public GameObject targetMeshObject;    // 不带骨骼的目标物体
        
        [Button()]
        public void Make()
        {
            
            SkinnedMeshRenderer sourceRenderer = sourceSkinnedObject.GetComponent<SkinnedMeshRenderer>();
            MeshFilter targetFilter = targetMeshObject.GetComponent<MeshFilter>();

            // 1. 复制骨骼层级
            Transform[] bones = new Transform[sourceRenderer.bones.Length];
            for (int i = 0; i < sourceRenderer.bones.Length; i++)
            {
                bones[i] = new GameObject(sourceRenderer.bones[i].name).transform;
                bones[i].parent = targetMeshObject.transform;
                bones[i].localPosition = sourceRenderer.bones[i].localPosition;
                bones[i].localRotation = sourceRenderer.bones[i].localRotation;
            }

            // 2. 创建 SkinnedMeshRenderer 并复制数据
            SkinnedMeshRenderer targetRenderer = targetMeshObject.AddComponent<SkinnedMeshRenderer>();
            Mesh newMesh = Instantiate(targetFilter.sharedMesh);
            newMesh.boneWeights = sourceRenderer.sharedMesh.boneWeights;
            newMesh.bindposes = sourceRenderer.sharedMesh.bindposes;

            // 3. 应用
            targetRenderer.sharedMesh = newMesh;
            targetRenderer.bones = bones;
            targetRenderer.rootBone = bones[0];
        }
        
        [Button()]
        public void CopyUv()
        {
            Mesh newMesh = Instantiate(renderer.sharedMesh);

            Debug.LogErrorFormat("CopyUv: {0} {1} {2} {3}", newMesh.uv, target.uv, newMesh.vertexCount, target.vertexCount);
            newMesh.uv = target.uv;
            
            renderer.sharedMesh = newMesh;
        }

        
        [Button()]
        public void ShowUv()
        {
            for (int i = 0; i < target.uv.Length; i++)
            {
                Debug.LogErrorFormat("uv: {0} {1}", i, target.uv[i]);
            }
        }

        
        [Button()]
        public void ShowVertice()
        {
            for (int i = 0; i < target.vertices.Length; i++)
            {
                Debug.LogErrorFormat("uv: {0} {1}", i, target.vertices[i]);
            }
        }
    }
}